﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class WaterTankCalculateCommand : HYNoTranExternalCommand
	{
		public override Result HYExecute()
		{
			WaterTankCalculate waterTankCalculate = new WaterTankCalculate(base.Doc);
			int num = (int)waterTankCalculate.ShowDialog();
			HYEquipmentCommon hyequipmentCommon = new HYEquipmentCommon();
			ICollection<ElementId> allFamilyInstance = hyequipmentCommon.GetAllFamilyInstance(base.Doc, BuiltInCategory.OST_MechanicalEquipment);
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			if (num == 1)
			{
				ListItemWaterTankPara currentTankPara = waterTankCalculate.currentTankPara;
				FamilySymbol familySymbol = null;
				if (familySymbol == null)
				{
					familySymbol = spaceFamilyDataReader.GetFamilySymbolById(int.Parse(waterTankCalculate.currentTankPara.id), "WaterTank", base.Doc, BuiltInCategory.OST_MechanicalEquipment);
				}
				if (familySymbol != null)
				{
					try
					{
						base.UiDoc.PromptForFamilyInstancePlacement(familySymbol);
					}
					catch (Exception)
					{
					}
				}
				Transaction transaction = new Transaction(base.Doc, "水箱布置");
				transaction.Start();
				ICollection<ElementId> allFamilyInstance2 = hyequipmentCommon.GetAllFamilyInstance(base.Doc, BuiltInCategory.OST_MechanicalEquipment);
				new List<ElementId>();
				foreach (ElementId item in allFamilyInstance.Intersect(allFamilyInstance2))
				{
					allFamilyInstance2.Remove(item);
				}
				foreach (ElementId elementId in allFamilyInstance2)
				{
					Element elementById = base.Doc.GetElementById(elementId);
					if (double.Parse(currentTankPara.width) > 0.0 && elementById.GetParameter("宽度") != null)
					{
						elementById.GetParameter("宽度").Set(double.Parse(currentTankPara.width) / 304.8);
					}
					if (double.Parse(currentTankPara.height) > 0.0 && elementById.GetParameter("高度") != null)
					{
						elementById.GetParameter("高度").Set(double.Parse(currentTankPara.height) / 304.8);
					}
					if (double.Parse(currentTankPara.length) > 0.0 && elementById.GetParameter("长度") != null)
					{
						elementById.GetParameter("长度").Set(double.Parse(currentTankPara.length) / 304.8);
					}
					if (double.Parse(currentTankPara.pipein) > 0.0 && elementById.GetParameter("进水管管径") != null)
					{
						elementById.GetParameter("进水管管径").Set(double.Parse(currentTankPara.pipein) / 304.8);
					}
					if (double.Parse(currentTankPara.pipeout) > 0.0 && elementById.GetParameter("出水管管径") != null)
					{
						elementById.GetParameter("出水管管径").Set(double.Parse(currentTankPara.pipeout) / 304.8);
					}
					if (double.Parse(currentTankPara.pipeoverflow) > 0.0 && elementById.GetParameter("溢流管管径") != null)
					{
						elementById.GetParameter("溢流管管径").Set(double.Parse(currentTankPara.pipeoverflow) / 304.8);
					}
					if (double.Parse(currentTankPara.pipeblowoff) > 0.0 && elementById.GetParameter("泄水管管径") != null)
					{
						elementById.GetParameter("泄水管管径").Set(double.Parse(currentTankPara.pipeblowoff) / 304.8);
					}
					if (double.Parse(currentTankPara.volume) > 0.0 && elementById.GetParameter("公称容积") != null)
					{
						elementById.GetParameter("公称容积").Set(UnitConvert.CovertToAPI(double.Parse(currentTankPara.volume), (Autodesk.Revit.DB.DisplayUnitType)14));
					}
					if (double.Parse(currentTankPara.weight) > 0.0 && elementById.GetParameter("水箱重量") != null)
					{
						elementById.GetParameter("水箱重量").Set(UnitConvert.CovertToAPI(double.Parse(currentTankPara.weight), (Autodesk.Revit.DB.DisplayUnitType)189));
					}
					SetParaForInstance.SetOffsetForInstance(elementById, waterTankCalculate.offset, base.Doc);
				}
				base.Doc.Regenerate();
				transaction.Commit();
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override void FilterProcess()
		{
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)1);
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)2);
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)4);
			base.FilterProcess();
		}
	}
}
